<template>
  <div class="app-container">
    <el-table v-loading="tableLoading" :data="tableData" border stripe style="margin-top: 20px; width: 100%">
      <el-table-column prop="content" label="内容"> </el-table-column>
      <el-table-column prop="handleResult" label="处理">
        <template slot-scope="scope">
          <el-button size="mini" v-if="!scope.row.isHandle" type="text" icon="el-icon-edit"
            @click="showDetails(scope.row)">去处理
          </el-button>
         <span v-else> {{scope.row.handleResult}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sendTime" label="添加时间" width="250px"> </el-table-column>
      <el-table-column prop="handleTime" label="处理时间"  width="180px"></el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="$refs.basicsForm.formValid()" />
    <el-dialog :title="item&&item.isHandle ? '详情' : '处理'" :visible.sync="openView" width="1100px" append-to-body>
      <basics-form style="margin-left: 100px" @selectCallback="selectCallback" ref="basicsAddForm" :minWidth="'800px'"
        :titleAligin="'right'" :titleWidth="'130px'" :actionLoading="loading" @actionBtnsClick="addAction"
        @submit="addSubmit" />
    </el-dialog>
  </div>
</template>

<script>
  import BasicsForm from "@/components/BasicsForm";
  import {
    remindList,
    careReasonListOfWeb,
    handleCareMsgOfWeb
  } from "@/api/manbing/message";
  export default {
    name: "patient-care",
    components: {
      BasicsForm
    },
    data() {
      return {
        loading: false,
        tableLoading: false,
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: 2
        },
        tableData: [],
        openView: false,
        item: null,
        careReason: null, //常见原因
      };
    },
    created() {},
    methods: {
      init(patientId) {
        this.tableData = []
        this.queryParams.pageNum = 1
        this.queryParams.patientId = patientId;
        this.$nextTick(() => {
          this.getList();
        });
      },
      //列表数据
      getList() {
        this.loading = true;
        this.tableLoading = true;
        remindList(this.queryParams)
          .then((res) => {
            if (res.code == 200) {
              this.tableData = res.rows;
              this.total = res.total;
            }
          })
          .finally(() => {
            this.loading = false;
            this.tableLoading = false;
          });
      },
      //设备详情
      showDetails(item) {
        this.openView = true;
        this.item = item;
        this.$nextTick(() => {
          this.getCareReasonListOfWeb();
        });
      },
      getCareReasonListOfWeb() {
        if (this.careReason) {
          this.initAddRules()
        } else {
          careReasonListOfWeb({}).then(res => {
            if (res.code == 200) {
              this.careReason = res.data
              this.initAddRules()
            }
          })
        }
      },
      initAddRules() {
        let rules = [{
            title: "内容",
            key: "content",
            value: this.item.content,
            disabled: true,
            skip: true,
          },
          {
            title: "常用语",
            key: "word",
            type: "select",
            selectList: this.careReason || [],
            hidden: this.item.isHandle ? true : false,
            selectKey: "dictValue",
            selectShowKey: "dictValue",
            skip: true,
            callback: true,
          },
          {
            title: "处理",
            key: "handleResult",
            type: 'textarea',
            value: this.item.handleResult || '',
            disabled: this.item.isHandle ? true : false,
          },
        ];
        if (!this.item.handleResult) {
          rules.push({
            type: "actionBtns",
            selectList: [{
                name: "取消",
                value: ""
              },
              {
                name: "提交",
                value: "primary"
              },
            ],
            btnsAligin: "center",
            ruleWidth: "903px",
          } )
        }
        this.$refs.basicsAddForm.initForm(rules);
      },
      //选择器回调
      selectCallback(res) {
        console.log(res, '---------')
        if (res.key == "word" && res.value) {
          this.$refs.basicsAddForm.changeParamValue({
            key: "handleResult",
            value: res.value
          });
        }
      },
      addAction(index) {
        if (index) {
          this.$refs.basicsAddForm.formValid();
        } else {
          this.openView = false;
        }
      },
      addSubmit(param) {
        if (!param) {
          return;
        }
        this.loading = true;
        param.reId = this.item.reId;
        handleCareMsgOfWeb(param)
          .then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("提交成功");
              this.openView = false;
              this.getList();
            }
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //
    },
  };
</script>
